package xymt.novaway.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import xymt.novaway.common.idempotent.annotation.RepeatSubmit;
import xymt.novaway.common.log.annotation.Log;
import xymt.novaway.common.web.core.BaseController;
import xymt.novaway.common.mybatis.core.page.PageQuery;
import xymt.novaway.common.core.domain.R;
import xymt.novaway.common.core.validate.AddGroup;
import xymt.novaway.common.core.validate.EditGroup;
import xymt.novaway.common.log.enums.BusinessType;
import xymt.novaway.common.excel.utils.ExcelUtil;
import xymt.novaway.system.domain.vo.SysTodoVo;
import xymt.novaway.system.domain.bo.SysTodoBo;
import xymt.novaway.system.service.ISysTodoService;
import xymt.novaway.common.mybatis.core.page.TableDataInfo;

/**
 * 待办事项
 *
 * @author NovaWay
 * @date 2025-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/todo")
public class SysTodoController extends BaseController {

    private final ISysTodoService sysTodoService;

    /**
     * 查询待办事项列表
     */
    @SaCheckPermission("system:todo:list")
    @GetMapping("/list")
    public TableDataInfo<SysTodoVo> list(SysTodoBo bo, PageQuery pageQuery) {
        return sysTodoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出待办事项列表
     */
    @SaCheckPermission("system:todo:export")
    @Log(title = "待办事项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysTodoBo bo, HttpServletResponse response) {
        List<SysTodoVo> list = sysTodoService.queryList(bo);
        ExcelUtil.exportExcel(list, "待办事项", SysTodoVo.class, response);
    }

    /**
     * 获取待办事项详细信息
     *
     * @param todoId 主键
     */
    @SaCheckPermission("system:todo:query")
    @GetMapping("/{todoId}")
    public R<SysTodoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long todoId) {
        return R.ok(sysTodoService.queryById(todoId));
    }

    /**
     * 新增待办事项
     */
    @SaCheckPermission("system:todo:add")
    @Log(title = "待办事项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTodoBo bo) {
        return toAjax(sysTodoService.insertByBo(bo));
    }

    /**
     * 修改待办事项
     */
    @SaCheckPermission("system:todo:edit")
    @Log(title = "待办事项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTodoBo bo) {
        return toAjax(sysTodoService.updateByBo(bo));
    }

    /**
     * 删除待办事项
     *
     * @param todoIds 主键串
     */
    @SaCheckPermission("system:todo:remove")
    @Log(title = "待办事项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{todoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] todoIds) {
        return toAjax(sysTodoService.deleteWithValidByIds(List.of(todoIds), true));
    }
}
